﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Runtime.InteropServices;

namespace ScoutWizard
{
    public partial class Form1 : Form
    {
        [DllImport("Kernel32.dll")]
        static extern bool Beep(
            uint dwFreq, uint dwDuration
        );

        private struct CharTextBoxTag
        {
            public int counter;
            public int Row;
            public int Column;
            public char _Char;
        }

        private string[] Frasi = null;
        private const int TextBoxWidth = 40;
        private const int TextBoxHeigh = 44;
        private const int TextBoxWidthGap = 5;
        private const int TextBoxHeighGap = 5;
        private Dictionary<char, Color> txtBoxColors = new Dictionary<char, Color>();
        private bool startstop = false;
        private DateTime StartTime = new DateTime();
        private int Duration = 30;// In second
        private int SecondsCounter = 0;
        private Color DefautBkColor = new Color();
        private int TextBoxCounter = 0;

        public Form1()
        {
            InitializeComponent();

            Properties.Settings Settings = new ScoutWizard.Properties.Settings();

            Frasi = new string[Settings.FrasesRows.Count];

            int i = 0;
            foreach(string Row in Settings.FrasesRows)
                Frasi[i++] = Row;

            txtBoxColors[' '] = Color.AliceBlue;
            txtBoxColors['!'] = Color.AntiqueWhite;
            txtBoxColors['"'] = Color.Aqua;
            txtBoxColors['#'] = Color.Aquamarine;
            txtBoxColors['$'] = Color.Azure;
            txtBoxColors['%'] = Color.Beige;
            txtBoxColors['&'] = Color.Bisque;
            txtBoxColors['\''] = Color.OldLace;
            txtBoxColors['('] = Color.BlanchedAlmond;
            txtBoxColors[')'] = Color.Blue;
            txtBoxColors['*'] = Color.BlueViolet;
            txtBoxColors['+'] = Color.Brown;
            txtBoxColors['\''] = Color.BurlyWood;
            txtBoxColors['-'] = Color.CadetBlue;
            txtBoxColors['.'] = Color.Chartreuse;
            txtBoxColors['/'] = Color.Chocolate;

            txtBoxColors['0'] = Color.Coral;
            txtBoxColors['1'] = Color.CornflowerBlue;
            txtBoxColors['2'] = Color.Cornsilk;
            txtBoxColors['3'] = Color.Crimson;
            txtBoxColors['4'] = Color.Cyan;
            txtBoxColors['5'] = Color.DarkCyan;
            txtBoxColors['6'] = Color.DarkGoldenrod;
            txtBoxColors['7'] = Color.DarkGray;
            txtBoxColors['8'] = Color.DarkGreen;
            txtBoxColors['9'] = Color.DarkKhaki;

            txtBoxColors[':'] = Color.DarkMagenta;
            txtBoxColors[';'] = Color.DarkOliveGreen;
            txtBoxColors['<'] = Color.DarkOrange;
            txtBoxColors['='] = Color.DarkOrchid;
            txtBoxColors['>'] = Color.DarkRed;
            txtBoxColors['?'] = Color.DarkSalmon;
            txtBoxColors['@'] = Color.DarkSeaGreen;

            txtBoxColors['A'] = Color.DarkSlateBlue;
            txtBoxColors['B'] = Color.DarkSlateGray;
            txtBoxColors['C'] = Color.DarkTurquoise;
            txtBoxColors['D'] = Color.DarkViolet;
            txtBoxColors['E'] = Color.DeepPink;
            txtBoxColors['F'] = Color.DeepSkyBlue;
            txtBoxColors['G'] = Color.DimGray;
            txtBoxColors['H'] = Color.DodgerBlue;
            txtBoxColors['I'] = Color.Firebrick;
            txtBoxColors['J'] = Color.FloralWhite;
            txtBoxColors['K'] = Color.ForestGreen;
            txtBoxColors['L'] = Color.Fuchsia;
            txtBoxColors['M'] = Color.Gainsboro;
            txtBoxColors['N'] = Color.GhostWhite;
            txtBoxColors['O'] = Color.Gold;
            txtBoxColors['P'] = Color.Goldenrod;
            txtBoxColors['Q'] = Color.Gray;
            txtBoxColors['R'] = Color.Green;
            txtBoxColors['S'] = Color.GreenYellow;
            txtBoxColors['T'] = Color.Honeydew;
            txtBoxColors['U'] = Color.HotPink;
            txtBoxColors['V'] = Color.IndianRed;
            txtBoxColors['W'] = Color.Indigo;
            txtBoxColors['X'] = Color.Ivory;
            txtBoxColors['Y'] = Color.Khaki;
            txtBoxColors['Z'] = Color.Lavender;

            txtBoxColors['['] = Color.LavenderBlush;
            txtBoxColors['\\'] = Color.LawnGreen;
            txtBoxColors[']'] = Color.LemonChiffon;
            txtBoxColors['^'] = Color.LightBlue;
            txtBoxColors['_'] = Color.LightCoral;
            txtBoxColors['^'] = Color.LightCyan;

            txtBoxColors['a'] = Color.LightGoldenrodYellow;
            txtBoxColors['b'] = Color.LightGray;
            txtBoxColors['c'] = Color.LightGreen;
            txtBoxColors['d'] = Color.LightPink;
            txtBoxColors['e'] = Color.LightSalmon;
            txtBoxColors['f'] = Color.LightSeaGreen;
            txtBoxColors['g'] = Color.LightSkyBlue;
            txtBoxColors['h'] = Color.LightSlateGray;
            txtBoxColors['i'] = Color.LightSteelBlue;
            txtBoxColors['j'] = Color.LightYellow;
            txtBoxColors['k'] = Color.Lime;
            txtBoxColors['l'] = Color.LimeGreen;
            txtBoxColors['m'] = Color.Linen;
            txtBoxColors['n'] = Color.Magenta;
            txtBoxColors['o'] = Color.Maroon;
            txtBoxColors['p'] = Color.MediumAquamarine;
            txtBoxColors['q'] = Color.MediumBlue;
            txtBoxColors['r'] = Color.MediumOrchid;
            txtBoxColors['s'] = Color.MediumPurple;
            txtBoxColors['t'] = Color.MediumSeaGreen;
            txtBoxColors['u'] = Color.MediumSlateBlue;
            txtBoxColors['v'] = Color.MediumSpringGreen;
            txtBoxColors['w'] = Color.MediumTurquoise;
            txtBoxColors['x'] = Color.MediumVioletRed;
            txtBoxColors['y'] = Color.MidnightBlue;
            txtBoxColors['z'] = Color.MintCream;

            txtBoxColors['{'] = Color.MistyRose;
            txtBoxColors['}'] = Color.Moccasin;
            txtBoxColors['|'] = Color.NavajoWhite;
            txtBoxColors['~'] = Color.Navy;

            Duration = Settings.Duration;

            this.WindowState = FormWindowState.Maximized;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int MaxWidth = 0;
            int CurrentTop = 15;
            
            for (int i = 0; i < Frasi.Count(); i++)
            {
                string Frase = Frasi[i];
                int CurrentLeft = TextBoxHeighGap;
                for (int n = 0; n < Frase.Length; n++)
                {
                    if (Frase[n] != ' ')
                    {
                        TextBox txtBox = new TextBox();
                        CharTextBoxTag Tag = new CharTextBoxTag();
                        Tag.Row = i;
                        Tag.Column = n;
                        Tag.counter = TextBoxCounter;
                        Tag._Char = Frase[n].ToString().ToUpper()[0];
                        txtBox.Tag = Tag;
                        txtBox.Name = TextBoxCounter.ToString();
                        txtBox.Parent = groupBox1;
                        txtBox.Top = CurrentTop;
                        txtBox.Left = CurrentLeft;
                        txtBox.Width = TextBoxWidth;
                        txtBox.Height = TextBoxHeigh;
                        txtBox.Font = new Font("Microsoft Sans Serif", 24.0f);
                        txtBox.TextAlign = HorizontalAlignment.Center;
                        txtBox.BackColor = txtBoxColors[Frase[n]];
                        txtBox.TextChanged += new EventHandler(txtBox_TextChanged);
                        txtBox.MaxLength = 1;
                        TextBoxCounter++;
                    }
                    CurrentLeft += TextBoxWidth + TextBoxWidthGap;
                    if (CurrentLeft > MaxWidth)
                        MaxWidth = CurrentLeft;
                }
                CurrentTop += TextBoxHeigh + TextBoxHeighGap;
            }
            groupBox1.Width = MaxWidth;
            groupBox1.Height = CurrentTop;

            DefautBkColor = BackColor;

            groupBox1.Enabled = false;

            groupBox1.Left = (this.Width) / 2 - groupBox1.Width / 2;
            groupBox1.Top = (this.Height - 100) / 2 - groupBox1.Height / 2;
        }

        void txtBox_TextChanged(object sender, EventArgs e)
        {
            TextBox dummyCtrl = (TextBox)sender;
            CharTextBoxTag Tag = (CharTextBoxTag)dummyCtrl.Tag;
            string NextFocusedCtrlName = (Tag.counter + 1).ToString();
            Control[] NextFocusedCtrl = this.Controls.Find(NextFocusedCtrlName, true);
            if (NextFocusedCtrl != null && NextFocusedCtrl.Count() != 0)
                NextFocusedCtrl[0].Focus();
            else
                ButtonStartStop.Focus();
        }

        private void CheckAndEnd()
        {
            timer1.Stop();
            ButtonStartStop.Text = "Start";
            BackColor = DefautBkColor;
            startstop = false;

            bool Win = true;
            for (int i = 0; i < TextBoxCounter; i++)
            {
                Control[] NextFocusedCtrl = this.Controls.Find(i.ToString(), true);
                if (NextFocusedCtrl != null && NextFocusedCtrl.Count() != 0)
                {
                    TextBox dummyTextBox = (TextBox)NextFocusedCtrl[0];
                    CharTextBoxTag Tag = (CharTextBoxTag)dummyTextBox.Tag;
                    if ((dummyTextBox.Text == null) || dummyTextBox.Text.Length == 0 || Tag._Char != dummyTextBox.Text.ToUpper()[0])
                    {
                        Win = false;
                        break;
                    }
                }
            }
            if (Win)
                StartWin();
            else
                StartLost();

            groupBox1.Enabled = false;
        }

        private void StartLost()
        {
            FormLost frmLost = new FormLost();
            frmLost.ShowDialog();
        }

        private void StartWin()
        {
            FormWin frmWin = new FormWin();
            frmWin.ShowDialog();
        }

        private void ButtonStartStop_Click(object sender, EventArgs e)
        {
            if (startstop == false)
            {
                ButtonStartStop.Text = "Stop";
                timer1.Start();
                StartTime = DateTime.Now;
                SecondsCounter = 0;
                startstop = true;
                groupBox1.Enabled = true;

                Control[] NextFocusedCtrl = this.Controls.Find("0", true);
                if (NextFocusedCtrl != null && NextFocusedCtrl.Count() != 0)
                {
                    NextFocusedCtrl[0].Focus();
                }
            }
            else
            {
                CheckAndEnd();
            }
        }

        int IndexBkColor = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            Beep(300,200);
            if (SecondsCounter >= txtBoxColors.Values.Count())
                IndexBkColor = 0;
            else
                IndexBkColor++;
            this.BackColor = txtBoxColors.Values.ElementAt(IndexBkColor);
            SecondsCounter++;
            LabelTime.Text = SecondsCounter.ToString() + "/" + Duration.ToString();
            if (SecondsCounter >= Duration)
            {
                CheckAndEnd();
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            groupBox1.Left = (this.Width) / 2 - groupBox1.Width / 2;
            groupBox1.Top = (this.Height - 100) / 2 - groupBox1.Height / 2;
            label1.Left = (this.Width) / 2 - label1.Width / 2;
        }
    }
}
